<?php

namespace App\Http\Controllers\Api;

use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use App\Models\Integral;
use App\Models\IntegralUsers;
use App\Models\Smsrecord;
use App\Models\Users;
use App\Models\YtkIntegral;
use App\Models\YtkIntegralTask;
use App\Models\YtkUsers;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Facades\JWTAuth;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\GetMobileRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;

class LoginController extends Controller
{

    //查看用户登录状态
    public function getUserStatus(Request $request)
    {
        $app = app('wechat.mini_program');
        $code = $request->get('code');
        $arr = $app->auth->session($code);
        $openid = $arr['openid'];
        //$openid = "oluCc6_vfQXRqPe-nEMhsXJfjZvQ";
        $users = Users::where('openid', $openid)->first();
        if (empty($users)) {
            $array['token'] = '';
            $array['program'] = $arr;
            $array['userInfo'] = '';
        } else {
            $user_id = Auth::guard('api')->id();
            $token = Auth::guard('api')->login($users);
            $array['token'] = $token;
            $array['userInfo'] = $users;
            $array['program'] = $arr;
            $array['user_id'] = $user_id;
        }

        //增加登录积分
        if ($array['token']) {
            $user_id = Auth::guard('api')->id();
            $integral = Integral::where('id', 1)->first();
            $nums = IntegralUsers::where(['user_id' => $user_id, 'integral_id' => 1])->whereDate('created_at', today())->count();
            if ($nums < $integral['total']) {
                $data['user_id'] = $user_id;
                $data['integral_id'] = 1;
                $data['integral'] = $integral['integral'];
                $data['status'] = 1;
                IntegralUsers::create($data);
                Users::where('id', $user_id)->increment('integral', $integral['integral']);
            }
        }
        return showMsg('获取成功', 200, $array);
    }

    //保存用户手机号
    public function setUsersMobile(Request $request)
    {
        $iv = $request->get('iv');
        $program = $request->get('program');
        $encryptedData = $request->get('encryptedData');
        $session = $program['session_key'];
        $openid = $program['openid'];
        $app = app('wechat.mini_program');
        $decryptedData = $app->encryptor->decryptData($session, $iv, $encryptedData);
        if (isset($decryptedData['phoneNumber'])) {
            //判断用户是否存在
            $users = Users::where('mobile', $decryptedData['phoneNumber'])->first();
            if (empty($users)) {
                $json['openid'] = $openid;
                $json['mobile'] = $decryptedData['phoneNumber'];
                $json['nickname'] = hide_string($decryptedData['phoneNumber']);
                $re = Users::create($json);
                $users = Users::where('id', $re->id)->first();
                $token = Auth::guard('api')->login($users);
                $array['token'] = $token;
                $array['userInfo'] = $users;
            } else {
                $token = Auth::guard('api')->login($users);
                $array['token'] = $token;
                $array['userInfo'] = $users;
            }
            return showMsg('获取成功', 200, $array);
        } else {
            return showMsg('获取失败', 201, $decryptedData);
        }
    }

}
